import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 蒋健
 * Date: 2022-02-07
 * Time: 13:50
 */
class TreeNode {
    public char val;
    public TreeNode left;//左孩子的引用
    public TreeNode right;//右孩子的引用

    public TreeNode(char val) {
        this.val = val;
    }
}

public class BinaryTree {
    public int count = 0;
    public TreeNode root;//二叉树的根节点

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        root = A;
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = H;
        return root;
    }

    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    /**
     * 获取树中节点的个数
     *
     * @param root
     * @return
     */
    int size(TreeNode root) {
        if (root == null) {
            return count;
        }
        count++;
        size(root.left);
        size(root.right);
        return count;
    }

    /**
     * 子问题思路
     */
    int size1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size1(root.right) + size1(root.left) + 1;
    }

    // 获取叶子节点的个数
    int LeafCount = 0;

    int getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.right == null && root.left == null) {
            LeafCount++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return LeafCount;
    }

    /**
     * 获取叶子节点的个数
     */
    int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.right == null && root.left == null) {
            //当前的root为叶子节点
            return 1;
        }

        return getLeafNodeCount(root.right) + getLeafNodeCount(root.left);
    }

    //获取第K层节点的个数
    //子问题思路
    int getKLevelNodeCount(TreeNode root, int k) {

        if (root == null || k <= 0) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    //获取二叉树的高度
    int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.right == null && root.left == null) {
            return 1;
        }
        return Math.max(getHeight(root.right), getHeight(root.left)) + 1;
    }

    int getHeight1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight1(root.left);
        int rightHeight = getHeight1(root.right);
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }

    /**
     * 检测值为value的元素是否存在
     */
    TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode ret = find(root.left,val);
        if (ret != null){
            return ret;
        }
        ret = find(root.right,val);
        if (ret != null){
            return ret;
        }
        return null;
    }
    /**
     * 判断一棵树是否为完全二叉树
     */
    boolean isCompleteTree(TreeNode root){
        if (root == null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (queue.peek() != null){
            queue.offer(queue.peek().left);
            queue.offer(queue.peek().right);
            queue.poll();
        }
        while (queue.poll() == null){
            if (queue.isEmpty()){
                return true;
            }
        }
        return false;
    }
//    boolean isCompleteTree(TreeNode root) {
//        if (root == null) {
//            return true;
//        }
//        Queue<TreeNode> queue = new LinkedList<>();
//        queue.offer(root);
//        while (!queue.isEmpty()){
//            TreeNode cur =queue.poll();
//            if (cur!=null){
//                queue.offer(cur.left);
//                queue.offer(cur.right);
//            }else {
//                break;
//            }
//        }
//        while (!queue.isEmpty()){
//            TreeNode top = queue.peek();
//            if (top!=null){
//                return false;
//            }
//            queue.poll();
//        }
//        return true;
//    }

}
